const ble = require("../../utils/bluetooth");
const util = require("../../utils/util");
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    sanyuan : false,
    tieli : false,
    nadian : false,
    qita : false,
    gaoyasanyuan: false,
    param_list : [
      { name: "过充电压保护", enable: true, address: 4365, name1: "过充阈值_mV", value1: "", address1: 4366, name2: "延迟判定_ms", value2: "", address2: 4367, name3: "释放阈值_mV", value3: "", address3: 4368, ratio1: 1, ratio2: 0.1, ratio3: 1},
      { name: "过放电压保护", enable: true, address: 4369, name1: "过放阈值_mV", value1: "", address1: 4370, name2: "延迟判定_ms", value2: "", address2: 4371, name3: "释放阈值_mV", value3: "", address3: 4372, ratio1: 1, ratio2: 0.1, ratio3: 1},
      { name: "深度放电保护", enable: true, address: 4373, name1: "阈值_mV", value1: "", address1: 4374, name2: "延迟判定_ms", value2: "", address2: 4375, name3: "释放阈值_mV", value3: "", address3: 4376, ratio1: 1, ratio2: 0.1, ratio3: 1},
      { name: "压差保护", enable: true, address: 4377, name1: "阈值_mV", value1: "", address1: 4378, name2: "延迟判定_ms", value2: "", address2: 4379, name3: "释放阈值_mV", value3: "", address3: 4380, ratio1: 1, ratio2: 0.1, ratio3: 1},
      { name: "二级过充保护", enable: true, address: 2050, name1: "阈值_mV", value1: "", address1: 2051, name2: "延迟判定_ms", value2: "", address2: 2052, name3: "释放阈值_mV", value3: "", address3: 2053, ratio1: 1, ratio2: 1, ratio3: 1},
      { name: "二级过放保护", enable: true, address: 2054, name1: "阈值_mV", value1: "", address1: 2055, name2: "延迟判定_ms", value2: "", address2: 2056, name3: "释放阈值_mV", value3: "", address3: 2057, ratio1: 1, ratio2: 1, ratio3: 1}
    ],
    modify_list : []
  },

  onLoad(options) {
    // if (ble.isConncted()) {
    //   this.readData()
    // }
  },

  async readData() {
    const { params } = await ble.getParamsWrapper(false)

    this.setData({
      param_list : params
    })
  },
  changeValue1(e) {
    const index = e.currentTarget.dataset.index; 
    const value =  e.detail.value;
    this.data.param_list[index].value1 = value
    this.addModifyList({
      address : e.currentTarget.dataset.address,
      value : value * e.currentTarget.dataset.ratio
    })
  },

  changeValue2(e) {
    const index = e.currentTarget.dataset.index; 
    const value =  e.detail.value;
    this.data.param_list[index].value2 = value
    this.addModifyList({
      address : e.currentTarget.dataset.address,
      value : value * e.currentTarget.dataset.ratio
    })
  },

  changeValue3(e) {
    const index = e.currentTarget.dataset.index; 
    const value =  e.detail.value ;
    this.data.param_list[index].value3 = value
    this.addModifyList({
      address : e.currentTarget.dataset.address,
      value : value * e.currentTarget.dataset.ratio
    })
  },

  handleSwitchChange(e) {
    const index = e.currentTarget.dataset.index;  
    const checked = e.detail;
    this.data.param_list[index].enable = checked
    this.setData({
      param_list : this.data.param_list
    })
    this.addModifyList({
      address : e.currentTarget.dataset.address,
      value : checked ? 1 : 0
    })
  },

  addModifyList(e) {
    var b_exists = false

    for (let i = 0; i < this.data.modify_list.length; i++) {
      const element = this.data.modify_list[i];
      if (element.address == e.address) {
        element.value = e.value
        b_exists = true
      }
    }

    if (!b_exists) {
      this.data.modify_list.push({
        address : e.address,
        value : e.value
      })
    }
  },

  async save() {
    if (!ble.isConncted()) {
      return;
    }

    wx.showLoading({
      title: '保存中...',
    })
    console.log(this.data.modify_list)

    for (let i = 0; i < this.data.modify_list.length; i++) {
      const element = this.data.modify_list[i];
      let address = element.address
      let value = parseInt(element.value)
      console.log("发送地址：",address, value)
      await ble.writeValueWrapper(address, value)
      await util.sleep(500)
    }

    wx.hideLoading()
    wx.showToast({
      title: '设置成功',
      icon: 'none'
    })
  },

  getDefaultValue(index) {
    let params = this.data.param_list
    if (index == 0) {
      params[0].value1 = 4200
      params[0].value2 = 1000
      params[0].value3 = 4100
      params[1].value1 = 2800
      params[1].value2 = 1000
      params[1].value3 = 2900
      params[2].value1 = 2650
      params[2].value2 = 500
      params[2].value3 = 2800
      params[3].value1 = 1000
      params[3].value2 = 1000
      params[3].value3 = 500
      params[4].value1 = 4230
      params[4].value2 = 500
      params[4].value3 = 4130
      params[5].value1 = 2700
      params[5].value2 = 500
      params[5].value3 = 2800
    } else if (index == 1) {
      params[0].value1 = 3700
      params[0].value2 = 1000
      params[0].value3 = 3550
      params[1].value1 = 2500
      params[1].value2 = 1000
      params[1].value3 = 2700
      params[2].value1 = 2250
      params[2].value2 = 500
      params[2].value3 = 2500
      params[3].value1 = 1000
      params[3].value2 = 1000
      params[3].value3 = 500
      params[4].value1 = 3750
      params[4].value2 = 500
      params[4].value3 = 3550
      params[5].value1 = 2300
      params[5].value2 = 500
      params[5].value3 = 2600
    } else if (index == 2) {
      params[0].value1 = 3950
      params[0].value2 = 1000
      params[0].value3 = 3850
      params[1].value1 = 1500
      params[1].value2 = 1000
      params[1].value3 = 1600
      params[2].value1 = 1450
      params[2].value2 = 500
      params[2].value3 = 1600
      params[3].value1 = 1000
      params[3].value2 = 1000
      params[3].value3 = 500
      params[4].value1 = 4050
      params[4].value2 = 500
      params[4].value3 = 3950
      params[5].value1 = 1400
      params[5].value2 = 500
      params[5].value3 = 1500
    } else if (index == 3) {
      params[0].value1 = 2800
      params[0].value2 = 1000
      params[0].value3 = 2750
      params[1].value1 = 2000
      params[1].value2 = 1000
      params[1].value3 = 2100
      params[2].value1 = 1800
      params[2].value2 = 500
      params[2].value3 = 2000
      params[3].value1 = 500
      params[3].value2 = 1000
      params[3].value3 = 300
      params[4].value1 = 2850
      params[4].value2 = 500
      params[4].value3 = 2750
      params[5].value1 = 1900
      params[5].value2 = 500
      params[5].value3 = 2000
    } else {
      params[0].value1 = 4300
      params[0].value2 = 1000
      params[0].value3 = 4250
      params[1].value1 = 2750
      params[1].value2 = 1000
      params[1].value3 = 2850
      params[2].value1 = 2650
      params[2].value2 = 500
      params[2].value3 = 2850
      params[3].value1 = 1000
      params[3].value2 = 1000
      params[3].value3 = 500
      params[4].value1 = 4350
      params[4].value2 = 500
      params[4].value3 = 4250
      params[5].value1 = 2700
      params[5].value2 = 500
      params[5].value3 = 2800
    }

    params[0].enable = true
    params[1].enable = true
    params[2].enable = true
    params[3].enable = true
    params[4].enable = true
    params[5].enable = true

    for (let i = 0; i < params.length; i++) {
      const element = params[i];
      this.addModifyList({
        address : element.address,
        value : element.enable ? 1 : 0
      })
      this.addModifyList({
        address : element.address1,
        value : element.value1
      })
      if (i <= 3 ) {
        this.addModifyList({
          address : element.address2,
          value : element.value2 * 0.1
        })
      } else {
        this.addModifyList({
          address : element.address2,
          value : element.value2
        })
      }
      
      this.addModifyList({
        address : element.address3,
        value : element.value3
      })
    }
    return params
  },

  choose(e) {
    console.log(e.currentTarget.dataset.index)
    const index = parseInt(e.currentTarget.dataset.index);
    if (index == 0) {
      this.setData({sanyuan:true,tieli:false,nadian:false,qita:false,gaoyasanyuan:false})
    } else if (index == 1) {
      this.setData({sanyuan:false,tieli:true,nadian:false,qita:false,gaoyasanyuan:false})
    } else if (index == 2) {
      this.setData({sanyuan:false,tieli:false,nadian:true,qita:false,gaoyasanyuan:false})
    } else if (index == 3){
      this.setData({sanyuan:false,tieli:false,nadian:false,qita:true,gaoyasanyuan:false})
    } else if (index == 4){
      this.setData({sanyuan:false,tieli:false,nadian:false,qita:false,gaoyasanyuan:true})
    }

    const params = this.getDefaultValue(index)
    this.setData({
      param_list : params
    })
  }
})